<template>
	<view class="mainpadding2" v-if="pageShow">
		<view class="mainpadding_top ffffff radius">
			<view class="flexbetween">
				<view class="titletext bold hei_text">印表機類型</view>
				<u-icon name="scan" color="#feb800" size="28" @click="saoma"></u-icon>
			</view>
			<view class="margin_top">
				<u-radio-group v-model="type" placement="row" circle>
					<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="發票印表機"
						:name="0"></u-radio>
					<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="收據印表機"
						:name="1"></u-radio>
					<u-radio activeColor="#ff8505" label="標籤印表機" :name="2"></u-radio>
				</u-radio-group>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="flexbetween">
				<view class="titletext bold hei_text">印表機型號</view>
				<view class="flexright">
					<u-radio-group v-model="data_type" placement="row" circle>
						<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="飛鵝"
							:name="1"></u-radio>
						<u-radio activeColor="#ff8505" label="芯燁" :name="2"></u-radio>
					</u-radio-group>
				</view>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius" v-if="type==2">
			<view class="titletext bold hei_text">紙張類型</view>
			<view class="margin_top">
				<u-radio-group v-model="bq_zz_type" placement="row" circle>
					<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="50*30" :name="1"></u-radio>
					<u-radio activeColor="#ff8505" label="40*25" :customStyle="{marginRight: '15rpx'}" :name="2"></u-radio>
					<u-radio activeColor="#ff8505" label="40*30" :customStyle="{marginRight: '15rpx'}" :name="3"></u-radio>
					<u-radio activeColor="#ff8505" label="35*25" :name="4"></u-radio>
				</u-radio-group>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius" v-if="type==0 || type==1">
			<view class="flexbetween">
				<view class="titletext bold hei_text">紙張類型</view>
				<view class="flexright">
					<u-radio-group v-model="zz_type" placement="row" circle>
						<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="58cm"
							:name="1"></u-radio>
						<u-radio activeColor="#ff8505" label="80cm" v-if="type==1" :name="2"></u-radio>
					</u-radio-group>
				</view>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius">
			<view class="flexbetween">
				<view class="titletext bold hei_text">設定大小字</view>
				<view class="flexright">
					<u-radio-group v-model="wzdx_type" placement="row" circle>
						<u-radio activeColor="#ff8505" :customStyle="{marginRight: '15rpx'}" label="小"
							:name="1"></u-radio>
						<u-radio activeColor="#ff8505" label="大" v-if="type==1" :name="2"></u-radio>
					</u-radio-group>
				</view>
			</view>
		</view>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">印表機名稱</view>
			<input class="main_size bold hei_text" type="text" placeholder="請輸入印表機名稱" style="text-align: right;"
				v-model="name">
		</view>

		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">小票機編碼</view>
			<input class="main_size bold hei_text" type="text" placeholder="請輸入小票機編碼" style="text-align: right;"
				v-model="code">
		</view>
		<view class="mainpadding_top ffffff radius flexbetween" v-if="data_type==1">
			<view class="titletext bold hei_text">小票機金鑰</view>
			<input class="main_size bold hei_text" type="text" placeholder="請輸入小票機金鑰" style="text-align: right;"
				v-model="secretkey">
		</view>
		<view class="mainpadding_top ffffff radius flexbetween">
			<view class="titletext bold hei_text">印表機開關</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="kg" activeColor="#feb800"></u-switch>
		</view>
		<view class="mainpadding_top ffffff radius flexbetween" v-if="type==0">
			<view class="titletext bold hei_text">交易明細</view>
			<u-switch inactiveColor="rgb(230, 230, 230)" v-model="fpmxly_open" activeColor="#feb800"></u-switch>
		</view>
		<view class="mainpadding posibottom" style="bottom: 0;">
			<view class="submitbtn" @click="submit">確認</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				shop_id: uni.getStorageSync("shop_id"), //当前店鋪id
				name: "", //印表機名称
				code: "", //编碼
				secretkey: "", //密钥
				type: 0, //类型:0=发票,1=收据,2=标签
				zz_type: 1, //紙張類型，1：58cm，2：80cm
				wzdx_type: 1, //文字大小,1小,2大
				id: "", //印表機id
				kg: false,
				fpmxly_open: false,
				pageShow: false,
				data_type: 1, //1=飞鹅,2=芯烨云
				issubmit: false
			}
		},
		onLoad(options) {
			uni.$on("scancode", data => {
				this.code = data[0]
				this.secretkey = data[1]
			})
			if (options.id) {
				uni.setNavigationBarTitle({
					title: "編輯印表機"
				})
				this.id = options.id
				this.getPrinterInfo()
			} else {
				this.pageShow = true
			}
		},
		watch: {
			type(newval) {
				if (newval != 1) {
					this.zz_type = 1
				}
				if (newval == 0 || newval == 2) {
					this.wzdx_type = 1
				}
			}
		},
		onUnload() {
			uni.$off("scancode")
		},
		methods: {
			saoma() {
				uni.navigateTo({
					url: "/pages/workbench/scancodedyj"
				})
			},
			async getPrinterInfo() { //印表機详情
				let _this = this
				let res = await this.http.request("/api/shop/Prindeail", 'POST', {
					shop_id: _this.shop_id,
					id: _this.id, //印表機id
				})
				if (res.code == 1) {
					this.type = Number(res.data.type)
					this.name = res.data.name
					this.code = res.data.code
					this.zz_type = Number(res.data.zz_type)
					this.bq_zz_type = Number(res.data.bq_zz_type)
					this.wzdx_type = Number(res.data.wzdx_type)
					this.data_type = Number(res.data.data_type)
					this.secretkey = res.data.secretkey
					this.kg = res.data.kg == 1 ? true : false
					this.fpmxly_open = res.data.fpmxly_open == 1 ? true : false
					this.pageShow = true
				} else if (res.code == 0) {
					this.http.toast(res.msg)
				} else {
					this.http.toast('系統處理中，請稍後再試');
				}
			},
			async submit() { //确定
				if (this.issubmit) return
				let _this = this
				let url = "",
					data = "",
					text = "";
				if (!this.name) {
					this.http.toast("請輸入印表機名稱！")
					return false
				}
				if (!this.code) {
					this.http.toast("請輸入小票機編碼！")
					return false
				}
				if (!this.secretkey && this.data_type == 1) {
					this.http.toast("請輸入小票機金鑰！")
					return false
				}
				this.issubmit = true
				if (!this.id) {
					url = "/api/shop/addPrin"
					data = {
						shop_id: _this.shop_id,
						name: _this.name, //印表機名称
						code: _this.code, //编碼
						secretkey: _this.secretkey, //密钥
						type: _this.type, //类型:0=发票,1=收据,2=标签
						zz_type: _this.zz_type,
						bq_zz_type: _this.bq_zz_type,
						wzdx_type: this.wzdx_type,
						data_type: this.data_type,
						kg: this.kg ? 1 : 0,
						fpmxly_open: this.fpmxly_open ? 1 : 0

					}
					text = "添加成功"
				} else {
					url = "/api/shop/editPrin"
					data = {
						shop_id: _this.shop_id,
						name: _this.name, //印表機名称
						code: _this.code, //编碼
						secretkey: _this.secretkey, //密钥
						type: _this.type, //类型:0=发票,1=收据,2=标签
						zz_type: _this.zz_type,
						bq_zz_type: _this.bq_zz_type,
						wzdx_type: this.wzdx_type,
						data_type: this.data_type,
						id: _this.id, //印表機id
						kg: this.kg ? 1 : 0,
						fpmxly_open: this.fpmxly_open ? 1 : 0
					}
					text = "修改成功"
				}
				let res = await this.http.request(url, 'POST', data)
				if (res.code == 1) {
					this.http.toast(text)
					setTimeout(() => {
						uni.navigateBack(1)
					}, 500)
				} else {
					this.issubmit = false
					this.http.toast(res.msg)
				}
			}
		}
	}
</script>

<style>

</style>